﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ECatalog.Models.Admin;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{

    public class AdminController : Controller
    {
        private readonly IUserRepository userRepository;
        private readonly IAdminRepository adminRepository;
        private readonly IAdminBuilder adminBuilder;

        public AdminController(IUserRepository userRepository, IAdminRepository adminRepository, IAdminBuilder adminBuilder)
        {
            this.userRepository = userRepository;
            this.adminRepository = adminRepository;
            this.adminBuilder = adminBuilder;
        }

        [Authorize]
        public ActionResult Index()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            return RedirectToAction("Index","Base");
        }

        [Authorize]
        public ActionResult Statistic()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            return RedirectToAction("Index","StatModel");
        }

        [AllowAnonymous]
        [HttpPost]
        public void Subscribe(SubscribeParams subscribeParams)
        {
            var validEmail = adminBuilder.Validate(subscribeParams.Write);
            if (validEmail)
            {
                adminRepository.SaveNotification(subscribeParams.Write, subscribeParams.Size, subscribeParams.ModelId, subscribeParams.ShopId);
            }
        }

        public ActionResult SendMail()
        {
            var result = adminBuilder.SendMail();
            ViewBag.Message = result;
            return View("Index");
        }
    }
}
